package com.domain.oa.controller;

import com.domain.oa.entity.Login;
import com.domain.oa.entity.User;
import com.domain.oa.service.impl.UserServiceImpl;
import com.domain.oa.utils.ResultVo;
import com.domain.oa.utils.VerificationCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户功能接口")
public class UserController {

    @Resource
    private UserServiceImpl userService;

    @ApiOperation("分页获取用户")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pageCurrent", value = "第几页", required = true, dataType = "Integer"),
                    @ApiImplicitParam(name = "pageSize", value = "一页多少", required = true, dataType = "Integer"),
                    @ApiImplicitParam(name = "userName", value = "用户名", required = true, dataType = "String", defaultValue = "")
            }
    )

    @GetMapping("/page")
    public ResultVo getUserByPage(@RequestParam Integer pageCurrent,
                                  @RequestParam Integer pageSize,
                                  @RequestParam(defaultValue = "") String userName) {
        return userService.getUserByPage(pageCurrent, pageSize, userName);
    }

    @ApiOperation("注册用户")
    @PostMapping("/register")
    public ResultVo registerUser(@RequestBody User user) {
        return userService.register(user.getPhone(), user.getPassword());
    }


    @ApiOperation("用户登录")
    @PostMapping("/login")
    public ResultVo userLogin(@RequestBody Login login,
                              HttpServletRequest request) {
        log.info(
                "存入session中的验证码：{}, 用户传入的验证码：{}",
                request.getSession(true).getAttribute("verify_code").toString(),
                login.getCode()
        );

        if(request.getSession(true).getAttribute("verify_code")==null||!login.getCode().toUpperCase().equals(request.getSession(true).getAttribute("verify_code").toString().toUpperCase())) {
            return new ResultVo(300, "验证码错误！", null);
        }

        return userService.checkLogin(login.getPhone(), login.getUsername(), login.getPassword());

    }

    @ApiOperation("获取验证码")
    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse resp) throws IOException {
        VerificationCode code = new VerificationCode();
        BufferedImage image = code.getImage();
        String text = code.getText();
        HttpSession session = request.getSession();
        session.setAttribute("verify_code", text);
        log.info("生成的verify_code: ----------{}", text);

        VerificationCode.output(image,resp.getOutputStream());
    }

    @ApiOperation("根据用户id获取缓存在Redis中的角色数据")
    @GetMapping("/userinfo/{userid}")
    public ResultVo getUserInfoByUserId(@PathVariable Integer userid) {
        return userService.getUserInfoByUserId(userid);
    }

    @ApiOperation("添加用户")
    @PutMapping()
    public ResultVo addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    @ApiOperation("编辑用户")
    @PostMapping
    public ResultVo editUser(@RequestBody User user) {
        return userService.editUser(user);
    }

    @ApiOperation("假删除用户")
    @DeleteMapping("/{userId}")
    public ResultVo pretendDelete(@PathVariable Integer userId) {
        return userService.pretendDelete(userId);
    }

}
